from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from . import serializers
from .models import Area
from rest_framework_extensions.cache.mixins import CacheResponseMixin

class AreaInfoView(ReadOnlyModelViewSet, CacheResponseMixin):
    # serializer_class = serializers.AreadInfoSerializer  # 序列化器
    # queryset = Area.objects.filter(parent=None)  # 过滤parent字段等于null  # 查询集

    def get_serializer_class(self):
        if self.action == "list":
            return serializers.AreadInfoSerializer
        else:
            return serializers.AreaSubsInfoSerializer

    def get_queryset(self):
        if self.action == "list":
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()
